﻿window.onload = function() {
    var doc = Common.Doc.documentElement;
    var div = Method.G(Common.DivDialog);
    var divAP = Method.L(div);
    var dc = new Editor_MouseClass();

    div.style.left = (doc.clientWidth - divAP.OffsetWidth) / 2 + doc.scrollLeft;
    div.style.top = (doc.clientHeight - divAP.OffsetHeight) / 2 + doc.scrollTop;

    document.onmousedown = function() { dc.Down(); };
    document.onmouseup = function() { dc.Up(); };
    document.onmousemove = function() { dc.Move(); };    
};

var Dialog =
{
    Tool: Common.Doc.Tool,
    Element: Common.Doc.Tool.Element,
    Content: function() { return Method.G(Common.DivContent); },
    Hide: function() { Common.Doc.Div.Hide(); Dialog.Tool.Focus(); },
    Family: function() {
        var f = ["宋体", "黑体", "楷体_GB2312", "隶书", "幼圆", "Arial",
            "Arial Black", "Comic Sans MS", "Courier", "System",
            "Times New Roman", "Verdana", "Georgia", "Tahoma"];
        var rows = f.length % 2 > 0 ? Math.floor(f.length / 2) + 1 : Math.floor(f.length / 2);
        var table = document.createElement("table");
        var tr, td, rad, span, label, k;

        for (var i = 0; i < rows; i++) {
            tr = table.insertRow();
            for (var j = 0; j < 2; j++) {
                k = i * 2 + j;
                td = tr.insertCell();
                rad = document.createElement(Method.F('<input type="radio" name="para" id="rad{0}" value="{1}" />', k, f[k]));
                span = document.createElement("span");
                label = document.createElement("label");

                span.style.cssText = Method.F("font-family: {0};", f[k]);
                label.htmlFor = rad.id;
                label.innerText = f[k];

                span.appendChild(label);
                td.appendChild(rad);
                td.appendChild(span);
            }
        }
        table.cellSpacing = 0;
        table.cellPadding = 5;
        table.width = "90%";
        table.style.margin = "10px";
        Dialog.Content().appendChild(table);
    },
    FamilyOK: function() {
        var rads = Dialog.Content().all.tags("INPUT");
        for (var i = 0; i < rads.length; i++) {
            if (rads[i].checked) {
                Dialog.Hide();
                Dialog.Tool.Cmd("FontName", rads[i].value);
                break;
            }
        }
    },
    FontSize: function() {
        var f = ["极小", "特小", "小", "中", "大", "特大", "极大"];
        var rows = f.length % 2 > 0 ? Math.floor(f.length / 2) + 1 : Math.floor(f.length / 2);
        var table = document.createElement("table");
        var tr, td, rad, font, label, k;
        for (var i = 0; i < rows; i++) {
            tr = table.insertRow();
            for (var j = 0; j < 2; j++) {
                k = i * 2 + j;
                if (k == f.length) break;
                td = tr.insertCell();
                rad = document.createElement(Method.F('<input type="radio" name="para" id="rad{0}" value="{1}" />', k, k + 1));
                font = document.createElement("font");
                label = document.createElement("label");

                font.size = k + 1;
                label.htmlFor = rad.id;
                label.innerText = f[k];

                font.appendChild(label);
                td.appendChild(rad);
                td.appendChild(font);
            }
        }
        table.cellSpacing = 0;
        table.cellPadding = 5;
        table.width = "90%";
        table.style.margin = "10px";
        Dialog.Content().appendChild(table);
    },
    FontSizeOK: function() {
        var rads = Dialog.Content().all.tags("INPUT");
        for (var i = 0; i < rads.length; i++) {
            if (rads[i].checked) {
                Dialog.Hide();
                Dialog.Tool.Cmd("FontSize", rads[i].value);
                break;
            }
        }
    },
    Color: function() {
        var tr, pTr, td, k;
        var tmp = ["预览颜色", "选中颜色", "颜色代码或名称"];
        var topTable = document.createElement("table");
        for (var i = 0; i < 2; i++) {
            tr = topTable.insertRow();
            if (i == 1) {
                tr.id = "trColor";
                pTr = tr;
            }
            for (var j = 0; j < 3; j++) {
                td = tr.insertCell();
                if (i == 0) td.innerText = tmp[j];
            }
        }
        pTr.cells[2].innerText = "#000000";
        topTable.cellSpacing = 1;
        topTable.className = "c_Table";
        Dialog.Content().appendChild(topTable);

        var c = ["00", "33", "66", "99", "CC", "FF"];
        var midTable = document.createElement("table");
        for (var i = 0; i < c.length; i++) {
            tr = midTable.insertRow();
            td = tr.insertCell();
            TdEvent(Method.F("cursor: pointer; font-size: 5px; padding: 5px 0; color: {0}{0}{0}; background-color: {0}{0}{0};", c[i]));
            for (var ii = 0; ii < c.length; ii++) {
                for (var iii = 0; iii < c.length; iii++) {
                    var color = "#" + c[i] + c[ii] + c[iii];
                    td = tr.insertCell();
                    TdEvent(Method.F("cursor: pointer; font-size: 5px; padding: 5px 0; color: {0}; background-color: {0};", color));
                }
            }
        }
        midTable.className = "dialogTable";
        Dialog.Content().appendChild(midTable);

        var cn = "绿色,酸橙色,森林绿,青色,靛青色,蓝色,紫色,红紫色,粟色,红色,暗灰色,黄色,白色,暗灰色,灰色,黑色"
        var en = "Green,Lime,Teal,Aqua,Navy,Blue,Purple,Fuchsia,Maroon,Red,Olive,Yellow,White,Silver,Gray,Black";
        var cnA = cn.split(",");
        var enA = en.split(",");
        var bottomTable = document.createElement("table");
        tr = bottomTable.insertRow();
        for (var i = 0; i < 16; i++) {
            td = tr.insertCell();
            TdEvent(Method.F("cursor: pointer; padding: 2px; background-color: {0}; color: {0}; width: {1}%;", enA[i], Math.floor(100 / 16)));
        }
        tr = bottomTable.insertRow();
        for (var i = 0; i < 16; i++) {
            td = tr.insertCell();
            td.innerText = cnA[i];
            td.style.cssText = "vertical-align: top; padding: 2px; text-align: center; background-color: #fff; word-break: break-all;"
        }
        bottomTable.className = "dialogTable";
        Dialog.Content().appendChild(bottomTable);

        function TdEvent(cssText) {
            td.innerText = "1";
            td.style.cssText = cssText;
            td.onclick = function() {
                pTr.cells[1].style.backgroundColor = this.style.backgroundColor;
                pTr.cells[2].innerText = this.style.backgroundColor.toUpperCase();
            };
            td.onmouseover = function() {
                pTr.cells[0].style.backgroundColor = this.style.backgroundColor;
            };
            td.onmouseout = function() {
                pTr.cells[0].style.backgroundColor = "";
            };
        };
    },
    ColorOK: function() {
        Dialog.Hide();
        Dialog.Tool.Cmd(Method.R("Type"), Method.G("trColor").cells[2].innerText);
    },
    LinkElement: function() {
        var Get =
        {
            Url: Method.G("txtUrl"),
            Type: Method.G("selType"),
            Open: Method.G("selOpen"),
            Text: Method.G("txtText"),
            Title: Method.G("txtTitle")
        };
        var Set = function(a) {
            var match = /\b(http:\/\/|https:\/\/|ftp:\/\/|news:\/\/|mailto:\/\/)/.exec(a.href);
            var type = match ? match[0] : "";

            Get.Url.value = a.href;
            Get.Text.value = a.innerHTML;
            Get.Title.value = a.title;
            for (var i = 0; i < Get.Type.options.length; i++) {
                if (Get.Type.options(i).text == type) Get.Type.selectedIndex = i;
            }
            for (var i = 0; i < Get.Open.options.length; i++) {
                if (Get.Open.options(i).value == a.target) Get.Open.selectedIndex = i;
            }
        };
        var Value =
        {
            Url: Get.Url.value,
            Type: Get.Type.options(Get.Type.selectedIndex).text,
            Open: Get.Open.options(Get.Open.selectedIndex).value,
            Text: Get.Text.value,
            Title: Get.Title.value
        };
        return { v: Value, g: Get, s: Set };
    },
    LinkChangeType: function() {
        var re = Dialog.LinkElement();
        var exp = /\b(http:\/\/|https:\/\/|ftp:\/\/|news:\/\/|mailto:\/\/)/;
        if (re.v.Url != "") {
            if (exp.test(re.v.Url)) {
                re.g.Url.value = re.v.Url.replace(exp, re.v.Type);
            } else {
                re.g.Url.value = re.v.Type + re.v.Url;
            }
        } else {
            re.g.Url.value = re.g.Type;
        }
    },
    LinkText: function() {
        var re = Dialog.LinkElement(), element = Dialog.Element;
        var text = Dialog.Tool.Range.htmlText;
        if (element.Parent.tagName == "A") {
            re.s(element.Parent);
        } else if (element.Self.tagName == "IMG") {
            re.g.Text.value = element.Self.outerHTML;
        } else if (text) {
            re.g.Text.value = text;
        } else {
            Dialog.Hide();
            alert("错误！");
        }
    },
    LinkOK: function() {
        var re = Dialog.LinkElement().v, element = Dialog.Element;
        Dialog.Tool.Focus();
        if (element.Parent.tagName == "A") {
            with (element.Parent) {
                href = re.Url;
                title = re.Title;
                target = re.Open;
                innerHTML = re.Text == "" ? re.Url : re.Text;
            }
        } else {
            try {
                var doc = Dialog.Tool.FrameDocument;
                var a = doc.createElement("a");
                a.href = re.Url;
                a.title = re.Title;
                a.target = re.Open;
                a.innerHTML = re.Text == "" ? re.Url : re.Text;
                if (element.Self.tagName == "IMG") {
                    doc.body.insertBefore(a, element.Self);
                    doc.body.removeChild(element.Self);
                } else {
                    Dialog.Tool.PasteHTML(a.outerHTML);
                }
            } catch (e) { alert("错误！"); };
        }
        Dialog.Hide();
    },
    ImageElement: function() {
        var Get =
        {
            Url: Method.G("txtUrl"),
            Text: Method.G("txtText"),
            Width: Method.G("txtWidth"),
            Height: Method.G("txtHeight"),
            Border: Method.G("txtBorder"),
            Div: Method.G("divPreview")
        };
        var Set = function(img) {
            Get.Url.value = img.src;
            Get.Text.value = img.alt;
            Get.Width.value = img.width;
            Get.Height.value = img.height;
            Get.Border.value = img.border;
            Get.Div.innerHTML = img.outerHTML;
        };
        var Value =
        {
            Url: Get.Url.value,
            Text: Get.Text.value,
            Width: Get.Width.value,
            Height: Get.Height.value,
            Border: Get.Border.value,
            Div: Get.Div.innerHTML
        };
        return { v: Value, g: Get, s: Set };
    },
    ImageText: function() {
        var re = Dialog.ImageElement(), element = Dialog.Element.Self;
        var text = Dialog.Tool.Range.htmlText;
        if (element) {
            if (element.tagName == "IMG") {
                setTimeout(function() { re.s(element); }, 100);
            } else if (text) {
                re.g.Text.value = text;
            }
        }
        for (e in re.g) {
            if (re.g[e].type == "text") {
                re.g[e].onblur = function() { Dialog.ImagePreview(); };
                if (e != "Url" && e != "Text") {
                    re.g[e].onkeydown = function() {
                        var keyCode = event.keyCode;
                        return (keyCode >= 33 && keyCode <= 58
                                || keyCode >= 96 && keyCode <= 105
                                || keyCode == 8) && !event.shiftKey;
                    };
                }
            }
        }
    },
    ImagePreview: function() {
        var re = Dialog.ImageElement();
        var img = document.createElement("img");
        if (re.v.Url != "") {
            img.src = re.v.Url;
            img.alt = re.v.Text;
            if (re.v.Width > 0) img.width = re.v.Width;
            if (re.v.Height > 0) img.height = re.v.Height;
            if (re.v.Border > 0) img.border = re.v.Border;
            re.g.Div.innerHTML = img.outerHTML;
        }
    },
    ImageOK: function() {
        var re = Dialog.ImageElement().v, element = Dialog.Element.Self;
        if (element.tagName == "IMG") {
            with (element) {
                src = re.Url;
                alt = re.Text;
                width = re.Width;
                height = re.Height;
                border = re.Border;
                Dialog.Tool.Focus();
            }
        } else {
            Dialog.Tool.PasteHTML(re.Div);
        }
        Dialog.Hide();
    },
    FaceLoad: function() {
        var tr, td;
        var table = document.createElement("table");
        for (var i = 0; i < 6; i++) {
            tr = table.insertRow();
            for (var ii = 0; ii < 10; ii++) {
                td = tr.insertCell();
                td.style.cssText = "text-align: center; cursor: pointer;";
                td.innerHTML = Method.F('<img src="..\\Faces\\QQ\\{0}.gif" alt="" />', ii + i * 10);
                td.onclick = function() {
                    Dialog.Tool.PasteHTML(this.innerHTML);
                };
            }
        }
        table.className = "dialogTable";
        Dialog.Content().appendChild(table);
    },
    VedioElement: function() {
        var Get =
        {
            Url: Method.G("txtUrl"),
            Flash: Method.G("radFlash"),
            Media: Method.G("radMedia"),
            Real: Method.G("radReal"),
            Yes: Method.G("radYes"),
            No: Method.G("radNo"),
            Loop: Method.G("txtLoop"),
            Height: Method.G("txtHeight"),
            Width: Method.G("txtWidth"),
            Div: Method.G("divPreview")
        };
        var Set = function(o) {
            var player = "等待选择类型...";
            var play = 0, loop = 0, url;

            switch (o.classid) {
                case "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000":  // Flash
                    player = VedioPlayer.Flash();
                    Get.Flash.checked = true;
                    if (o.object.Playing == "1") {
                        Get.Yes.checked = true;
                    } else {
                        Get.No.checked = true;
                    }
                    url = o.object.Movie;
                    play = o.object.Playing;
                    loop = o.object.Loop;
                    break;
                case "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6": // Media
                    player = VedioPlayer.Media();
                    Get.Media.checked = true;
                    if (o.object.settings.autoStart) {
                        Get.Yes.checked = true;
                    } else {
                        Get.No.checked = true;
                    }
                    url = o.object.url;
                    play = o.object.settings.autoStart;
                    loop = o.object.settings.playCount;
                    break;
                case "clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA": // Real
                    player = VedioPlayer.Real();
                    Get.Real.checked = true;
                    if (o.object.AutoStart) {
                        Get.Yes.checked = true;
                    } else {
                        Get.No.checked = true;
                    }
                    url = o.object.Source;
                    play = o.object.AutoStart;
                    loop = o.object.GetNumLoop();
                    break;
            }
            Get.Url.value = url;
            Get.Loop.value = loop;
            Get.Height.value = o.height;
            Get.Width.value = o.width;
            Get.Div.innerHTML = Method.F(player, url, play, loop, o.height, o.width);
        };
        var Value =
        {
            Url: Get.Url.value,
            Flash: Get.Flash.checked,
            Media: Get.Media.checked,
            Real: Get.Real.checked,
            Yes: Get.Yes.checked,
            No: Get.No.checked,
            Loop: Get.Loop.value,
            Height: Get.Height.value,
            Width: Get.Width.value,
            Div: Get.Div.innerHTML
        };
        return { v: Value, g: Get, s: Set };
    },
    VedioText: function() {
        var re = Dialog.VedioElement(), element = Dialog.Element.Self;
        var text = Dialog.Tool.Range.htmlText;
        if (element) {
            if (element.tagName == "OBJECT") {
                re.g.Flash.disabled = re.g.Media.disabled = re.g.Real.disabled = true;
                setTimeout(function() { re.s(element); }, 100);
            } else {
                re.g.Flash.disabled = re.g.Media.disabled = re.g.Real.disabled = false;
            }
        }
        for (e in re.g) {
            if (re.g[e].tagName == "INPUT") {
                re.g[e].onblur = function() { Dialog.VedioPreview(); };
                if (e != "Url" && re.g[e].type == "text") {
                    re.g[e].onkeydown = function() {
                        var keyCode = event.keyCode;
                        return (keyCode >= 33 && keyCode <= 58
                                || keyCode >= 96 && keyCode <= 105
                                || keyCode == 8) && !event.shiftKey;
                    };
                }
            }
        }
    },
    VedioPreview: function() {
        var play = 0;
        var player = VedioPlayer.Flash();
        var re = Dialog.VedioElement();
        if (re.v.Media) player = VedioPlayer.Media();
        if (re.v.Real) player = VedioPlayer.Real();
        if (re.v.Yes) play = 1;
        re.g.Div.innerHTML = Method.F(player, re.v.Url, play, re.v.Loop, re.v.Height, re.v.Width);
    },
    VedioOK: function() {
        var re = Dialog.VedioElement().v, element = Dialog.Element.Self;
        if (element.tagName == "OBJECT") {
            with (element) {
                switch (element.classid) {
                    case "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000":  // Flash
                        object.Movie = re.Url;
                        object.Playing = re.Yes ? 1 : 0;
                        object.Loop = re.Loop;
                        break;
                    case "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6": // Media
                        object.url = re.Url;
                        object.settings.autoStart = re.Yes ? 1 : 0;
                        object.settings.playCount = re.Loop;
                        break;
                    case "clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA": // Real
                        object.Source = re.Url;
                        object.AutoStart = re.Yes ? 1 : 0;
                        object.SetNumLoop(re.Loop);
                        break;
                }
                width = re.Width;
                height = re.Height;
                Dialog.Tool.Focus();
            }
        } else {
            Dialog.Tool.PasteHTML(re.Div);
        }
        Dialog.Hide();
    }
};

var VedioPlayer =
{
    Flash: function() {
        var code = '';
        code += '<object id="flashPlayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" height="{3}" width ="{4}">';
        code += '<param name="Movie" value="{0}" />';
        code += '<param name="Playing" value="{1}" />';
        code += '<param name="Loop" value="{2}" />';
        code += '</object>';
        return code;
    },
    Media: function() {
        var code = '';
        code += '<object id="mediaPlayer" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" height="{3}" width ="{4}">';
        code += '<param name="autoStart" value="{1}" />';
        code += '<param name="url" value="{0}" />';
        code += '<param name="playCount" value="{2}" />';
        code += '<param name="enabled" value="1" />';
        code += '<param name="enabledContextMenu" value="0" />';
        code += '<param name="uiMode" value="Full" />';
        code += '</object>';
        return code;
    },
    Real: function() {
        var code = '';
        code += '<object id="realPlayer" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" height="{3}" width ="{4}">';
        code += '<param name="AutoStart" value ="{1}" />';
        code += '<param name="Source" value="{0}" />';
        code += '<param name="Loop" value="1" />';
        code += '<param name="NumLoop" value="{2}" />';
        code += '<param name="Center" value ="1" />';
        code += '<param name="Console" value="rp" />';
        code += '<param name="NoLabels" value="1" />';
        code += '<param name="NoJava" value="1" />';
        code += '<param name="NoLogo" value="1" />';
        code += '<param name="ImageStatus" value="1" />';
        code += '<param name="MainTainaspect" value="1" />';
        code += '<param name="Controls" value="All" />';
        code += '</object>';
        return code;
    }
};